import { createStore } from 'vuex'
import router from '@/router'
import Admin from "./admin"
export default createStore({
  state() {
    return {
      routerIndex: 0,//代表当前使用的页面
      routerMenus: [
        {
          name: "首页",
          path: "/index",
          close: false
        }
      ]
    }
  },
  getters: {
  },
  mutations: {
    deleteRouterMenuItem(state, idx) {
      state.routerMenus.splice(idx, 1)
      if (state.routerIndex == idx) {
        state.routerIndex = 0 // 索引归0 代表显示首页
        router.push("/index")
      } else if(idx<state.routerIndex){
        return state.routerIndex--
        
      }


    },
    addrouterMenuItem(state, item) {
      let has = false;
      // console.log(this.state);
      for (let i = 0; i < state.routerMenus.length; i++) {
        const elem = state.routerMenus[i]
        if (elem.path == item.path) {
          has = true
          break
        }
      }
      if (!has) {
        state.routerIndex = state.routerMenus.length// 索引设置为当前添加的元素
        state.routerMenus.push({
          name: item.name,
          path: item.path,
          close: Boolean(item.path),
        })
        // console.log(state.routerMenus);
      }
    },
    initRouterMenus(state, routerMenus) {
      if (routerMenus.length > 1) {
        state.routerMenus = routerMenus
      }
    },
    initRouterIndex(state, index) {
      if (index > 0) {
        state.routerIndex = index
      }
    }
  },
  actions: {
  },
  modules: {
    Admin
  }
})
